package com.cuit.controller;

import com.cuit.mapper.BookMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.OrderInfo;
import com.cuit.enums.OrderStatus;
import com.cuit.service.FileService;
import com.cuit.service.OrderInfoService;
import com.cuit.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //开放前端的跨域访问
@Api(tags = "商品订单管理")
@Controller
@RequestMapping("/order-info")
public class OrderInfoController {

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private BookMapper bookMapper;

    @Resource
    private FileService fileService;

    @ApiOperation("所有订单列表")
    @GetMapping("/order/list")
    public String list(ModelMap modelMap){

        List<OrderInfo> list = orderInfoService.listOrderByCreateTimeDesc();
        modelMap.addAttribute("orderList",list);
        return "admin/order-info";
    }


    @ApiOperation("用户订单列表")
    @GetMapping("/list/{userId}")
    @ResponseBody
    public R listById(@PathVariable Long userId){
        List<OrderInfo> list = orderInfoService.listOrderByCreateTimeDescById(userId);
        list.forEach(order->{
            Book book = bookMapper.selectById(order.getProductId());
            order.setOrderNo(fileService.allPath(book.getBookimg()));
        });
        return R.ok().data("list", list);
    }


    /**
     * 查询本地订单状态
     * @param orderNo
     * @return
     */
    @ApiOperation("查询本地订单状态")
    @GetMapping("/query-order-status/{orderNo}")
    @ResponseBody
    public R queryOrderStatus(@PathVariable String orderNo){

        String orderStatus = orderInfoService.getOrderStatus(orderNo);
        if(OrderStatus.SUCCESS.getType().equals(orderStatus)){
            return R.ok().setMessage("支付成功"); //支付成功
        }

        return R.ok().setCode(101).setMessage("支付中......");
    }



}
